package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.GetScriptResponse;
import com.zzhoujay.richtext.RichText;

import java.util.List;
import java.util.stream.Collectors;

public class ScriptDialog extends Dialog {

    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle, tvContent, tvNoData, tvReaded;
    private ImageView ivClose, ivMenu;
    private LinearLayout llMenu;

    private List<GetScriptResponse.Data> dataList;
    private List<String> titleList;
    private int selectPosition = 0;

    private MenuPopup mMenuPopup;
    private Callback mCallback;

    public ScriptDialog(@NonNull Context context, Callback callback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = callback;
        initContentView();
        init(context);
        RichText.initCacheDir(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_script, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        tvContent = dialogView.findViewById(R.id.tvContent);
        tvNoData = dialogView.findViewById(R.id.tvNoData);
        ivClose = dialogView.findViewById(R.id.ivClose);
        tvReaded = dialogView.findViewById(R.id.tvReaded);
        ivMenu = dialogView.findViewById(R.id.ivMenu);
        llMenu = dialogView.findViewById(R.id.llMenu);

        ivClose.setOnClickListener(v -> {
            RichText.clear(this);
            cancel();
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void initData(List<GetScriptResponse.Data> scriptList) {
        dataList = scriptList;
        if (null != scriptList && scriptList.size() > 0) {
            tvContent.setVisibility(View.VISIBLE);
            tvNoData.setVisibility(View.GONE);
            tvReaded.setEnabled(true);
            //该方法6.0不兼容
            tvContent.setText("");
            titleList = scriptList.stream().map(GetScriptResponse.Data::getTitle).collect(Collectors.toList());
            tvTitle.setText(scriptList.get(selectPosition).getTitle());
            RichText.from(scriptList.get(selectPosition).getContent()).bind(mContext).into(tvContent);

            ivMenu.setOnClickListener(v -> {
                mMenuPopup = new MenuPopup(mContext, titleList, selectPosition, new MenuPopup.OnSelectListener() {
                    @Override
                    public void onSelect(int position) {
                        selectPosition = position;
                        tvContent.setText("");
                        tvTitle.setText(scriptList.get(position).getTitle());
                        if (!TextUtils.isEmpty(scriptList.get(position).getContent())) {
                            tvContent.setVisibility(View.VISIBLE);
                            tvNoData.setVisibility(View.GONE);
                            RichText.from(scriptList.get(position).getContent()).bind(mContext).into(tvContent);
                        } else {
                            tvContent.setVisibility(View.GONE);
                            tvNoData.setVisibility(View.VISIBLE);
                        }
                        if (scriptList.get(position).isRead()) {
                            tvReaded.setEnabled(false);
                        } else {
                            tvReaded.setEnabled(true);
                        }
                        mMenuPopup.dismiss();
                    }
                });
                mMenuPopup.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                mMenuPopup.setHeight(mMenuPopup.getContentView().getMeasuredHeight());
                int[] location = new int[2];
                llMenu.getLocationInWindow(location);
                if (!mMenuPopup.isShowing()) {
                    mMenuPopup.showAtLocation(llMenu, Gravity.NO_GRAVITY,
                            location[0], location[1] - mMenuPopup.getHeight());
                }
            });

            if (scriptList.get(selectPosition).isRead()) {
                tvReaded.setEnabled(false);
            } else {
                tvReaded.setEnabled(true);
            }

            tvReaded.setOnClickListener(v -> {
                if (null != mCallback) {
                    mCallback.onReaded(selectPosition);
                }
            });
        } else {
            tvContent.setVisibility(View.GONE);
            tvNoData.setVisibility(View.VISIBLE);
            tvReaded.setEnabled(false);
        }
    }

    public void refreshReaded(int position) {
        if (dataList.get(position).isRead()) {
            tvReaded.setEnabled(false);
        } else {
            tvReaded.setEnabled(true);
        }
    }

    public interface Callback {
        void onReaded(int position);
    }
}
